#!/bin/bash

# tidy_lsvpd_dbs - Tidy up lsvpd databases

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2003, 2004, 2005

# Maintained by Martin Schwenke <martins@au.ibm.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
# $Id: tidy_lsvpd_dbs.in,v 1.1 2006/04/11 18:38:28 emunson Exp $

######################################################################

if [ -z "$LSVPD_LIBDIR" ] ; then
    LSVPD_LIBDIR="/lib/lsvpd" ; export LSVPD_LIBDIR
fi

PATH="${LSVPD_LIBDIR}:/sbin:${PATH}:/usr/sbin:/usr/local/sbin" ; export PATH

. "${LSVPD_LIBDIR}/lsvpd-functions.bash"

shopt -s nullglob

######################################################################

do_setup

# Sanity checks.

# We end up hardcoding "db" a lot below to make things more readable.
if [ "$db_prefix" != "db" ] ; then
    echo "$0: unexpected \$db_prefix=\"$db_prefix\"" >&2
    exit 1
fi

if [ -z "$vardir" ] ; then
    echo "$0: unexpected unset variable \$var_dir" >&2
    exit 1
fi

######################################################################


# Ensure that update-lsvpd-db has been run.
[ -L "${vardir}/${db_prefix}" ] || /sbin/update-lsvpd-db

# This would be very unexpected.
if [ ! -L "${vardir}/${db_prefix}" ] ; then
    echo "$0: unable to initialise database at \"${vardir}/${db_prefix}\"" >&2
    exit 1
fi

######################################################################

# This is to workaround the fact that tar doesn't like filenames with
# colons in them, since it thinks that the things that precede colons
# are hostnames.  Later versions of update-lsvpd-db don't create
# directory names with colons in them, so this isn't an ongoing
# problem.
#
# This could be done in a post-install script but is more neatly done
# here, since it needs to be done on all platforms.
for i in "${vardir}/"device-tree-* ; do
    if [ -d "$i" -a ! -L "$i" ] ; then
	case "$i" in
	    *:*)
		mv "$i" $(echo "$i" | sed -e 's/://g')
	esac
    fi
done

######################################################################

# THE MAIN EVENT

tidy_subdirs "$vardir" "^device-tree-"
tidy_subdirs "$vardir" "^db-"

######################################################################

# REMOVAL OLD ARCHIVES AND CORRESPONDING SYMLINKS.

mindays=30  # Minimum days to keep archives for.
mincount=10 # Minimum number of archives to keep.

# Loop from oldest to newest, remembering each and counting.
count=0
archives=""
for i in "${vardir}/"device-tree-*.tar.gz "${vardir}/"db-*.tar.gz ; do
    if [ ! -L "$i" ] ; then
	archives="${archives} ${i}"
	count=$((count + 1))
    fi
done

# Remove the ones we don't want.
for i in $archives ; do
    # If we don't have enough to throw any away, we're done.
    [ $count -le $mincount ] && break

    # If $i is older than $mindays then delete it.
    x=$(find $i -type f -mtime +$mindays)
    if [ -n "$x" ] ; then 
	rm -f  $i
	count=$(($count - 1))
    fi
done

# Check for dead symlinks.
for i in "${vardir}/"device-tree-*.tar.gz "${vardir}/"db-*.tar.gz ; do
    [ -L $i -a ! -r $i ] && rm -f $i
done

######################################################################

# REMOVE OLD DIRECTORIES AND CORRESPONDING SYMLINKS.

# Very old database location: unlikely to be useful.
olddb=/var/lib/device-tree
[ -f "${olddb}/linux,vpd" ] && rm -rf "$olddb"

# Check this one manually.  This was an old, broken safety net.
i="${vardir}/device-tree-0000-00-00-00:00:00"
[ -L $i -a ! -r $i ] && rm -f $i

# Need to clean up old device-tree-* directory if we have enough db-*
# archives.  There should only be one, but it doesn't really matter.
for d in "${vardir}/"device-tree-* ; do
    if [ -d $d -a ! -L $d ] ; then
	# This could use some cached information from above to make
	# this more efficient.  However, we'll probably only ever do
	# this once and remove the old directory.
	count=0
	for i in "${vardir}/"db-*.tar.gz ; do
	    [ ! -L "$i" ] && count=$((count + 1))
	done

	# Enough archives?  Directory contains something sane?  Remove it.
	[ $count -ge $mincount -a -f "${d}/linux,vpd" ] && \
	    rm -rf $d
    fi
done

# Might now have a dead device-tree link.
i="${vardir}/device-tree"
[ -L $i -a ! -r $i ] && rm -f $i

######################################################################

exit 0
